
#import "UIViewController+HUD.h"
#import "MBProgressHUD.h"
#import <objc/runtime.h>

//无法获取您当前地理位置
static const void *kUniqueHUDKey = &kUniqueHUDKey;
@implementation UIViewController (UniqueHUD)

- (MBProgressHUD *)uniqueHUD {
    MBProgressHUD *hud = nil;
    @synchronized (self) {
        hud = objc_getAssociatedObject(self, kUniqueHUDKey);
        if (!hud) {
            hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            objc_setAssociatedObject(self, kUniqueHUDKey, hud, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
            hud.userInteractionEnabled = NO;
            // Configure for text only and offset down
            hud.mode = MBProgressHUDModeText;
            hud.margin = 10.f;
            hud.removeFromSuperViewOnHide = YES;
        }
    }
    return hud;
}

- (void)hideUHUD {
    MBProgressHUD *hud = [self uniqueHUD];
    if (hud) {
        [hud hide:YES];
    }
}

- (void)showUHint:(NSString *)tipString completion:(void(^)(void))completion {
    [self showUHint:tipString inView:self.view completion:completion];
}
- (void)showUHint:(NSString *)tipString inView:(UIView *)view completion:(void(^)(void))completion {
    [self showUHint:tipString inView:view yOffset:0 completion:completion];
}
- (void)showUHint:(NSString *)tipString inView:(UIView *)view yOffset:(CGFloat)y completion:(void(^)(void))completion {
    MBProgressHUD *hud = [self uniqueHUD];
    hud.mode = MBProgressHUDModeText;
    hud.labelText = tipString;
    hud.removeFromSuperViewOnHide = YES;
    hud.yOffset = y;
    hud.completionBlock = ^(void) {
        if (completion) {
            completion();
        }
    };
    if (hud.superview) {
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hideUHUD) object:nil];
    }
    else {
        [view addSubview:hud];
        [hud show:YES];
    }
    
    [self performSelector:@selector(hideUHUD) withObject:nil afterDelay:2.0];
}

- (void)showHint:(NSString *)hint completion:(void(^)(void))completion {
    [self showHint:hint inView:self.view completion:completion];
}
- (void)showHint:(NSString *)hint inView:(UIView *)view completion:(void(^)(void))completion {
    [self showHint:hint inView:view yOffset:0 completion:completion];
}
- (void)showHint:(NSString *)hint inView:(UIView *)view yOffset:(CGFloat)y completion:(void(^)(void))completion {
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
    hud.labelText = hint;
    hud.completionBlock = ^(void){
        if (completion) {
            completion();
        }
    };
    if (view!=hud.superview) {
        [view addSubview:hud];
    }
    [hud show:YES];
    [hud hide:YES afterDelay:2];
}

@end

#pragma mark - UIViewController (Image)
@implementation UIViewController (Image)

- (void)showSuccessHint:(NSString *)tipString {
    [self showSuccessHint:tipString completion:nil];
}
- (void)showSuccessHint:(NSString *)tipString completion:(void(^)(void))completion {
    [self showSuccessHint:tipString inView:self.view completion:completion];
}
- (void)showSuccessHint:(NSString *)tipString inView:(UIView *)view completion:(void(^)(void))completion {
    [self showSuccessHint:tipString inView:view yOffset:0 completion:completion];
}
- (void)showSuccessHint:(NSString *)tipString inView:(UIView *)view yOffset:(CGFloat)y completion:(void(^)(void))completion {
    [self showHint:tipString image:[UIImage imageNamed:@"Hud_Success"] inView:view yOffset:y completion:completion];
}

- (void)showErrorHint:(NSString *)tipString {
    [self showErrorHint:tipString completion:nil];
}
- (void)showErrorHint:(NSString *)tipString completion:(void(^)(void))completion {
    [self showErrorHint:tipString inView:self.view completion:completion];
}
- (void)showErrorHint:(NSString *)tipString inView:(UIView *)view completion:(void(^)(void))completion {
    [self showErrorHint:tipString inView:view yOffset:0 completion:completion];
}
- (void)showErrorHint:(NSString *)tipString inView:(UIView *)view yOffset:(CGFloat)y completion:(void(^)(void))completion {
    [self showHint:tipString image:[UIImage imageNamed:@"Hud_Fail"] inView:view yOffset:y completion:completion];
}

- (void)showHint:(NSString *)tipString image:(UIImage *)image inView:(UIView *)view yOffset:(CGFloat)y completion:(void(^)(void))completion {
    
    MBProgressHUD *(^MBHud)(void) = ^(void) {
        MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
        hud.mode = MBProgressHUDModeCustomView;
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        hud.customView = imageView;
        hud.detailsLabelFont = [UIFont boldSystemFontOfSize:14.0f];
        return hud;
    };
    
    MBProgressHUD *hud = MBHud();
    hud.yOffset = y;
    hud.detailsLabelText = tipString;
    if (view!=hud.superview) {
        [view addSubview:hud];
    }
    if ([NSThread isMainThread]) {
        hud.completionBlock = ^(){
            if (completion) {
                completion();
            }
        };
        [hud show:YES];
        [hud hide:YES afterDelay:2];
    }
    else{
        dispatch_sync(dispatch_get_main_queue(), ^(){
            [hud show:YES];
            [hud hide:YES afterDelay:2];
            hud.completionBlock = ^(){
                if (completion) {
                    completion();
                }
            };
        });
    }
}

@end

static char kHudGlobalKey = '\0';
@implementation UIViewController (Loading)
- (void)showLoadingHudWithHint:(NSString *)hint withBlock:(BOOL)block {
    MBProgressHUD *hud = objc_getAssociatedObject(self, &kHudGlobalKey);
    if (!hud) {
        hud = [MBProgressHUD showHUDAddedTo:block?[UIApplication sharedApplication].keyWindow:self.view animated:YES];
        hud.removeFromSuperViewOnHide = YES;
        hud.mode = MBProgressHUDModeIndeterminate;
        objc_setAssociatedObject(self, &kHudGlobalKey, hud, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    hud.detailsLabelText = hint;
    [block?[UIApplication sharedApplication].keyWindow:self.view addSubview:hud];
    [hud show:YES];
}

- (void)dismissLoadingHud {
    MBProgressHUD *hud = objc_getAssociatedObject(self, &kHudGlobalKey);
    if (hud) {
        [hud hide:YES];
    }
}

@end

